package org.praktika.api.service;

import com.vaadin.exampledata.DataType;
import com.vaadin.exampledata.ExampleDataGenerator;
import lombok.RequiredArgsConstructor;
import org.praktika.api.dto.GenerationDataDto;
import org.praktika.api.entity.City;
import org.praktika.api.entity.Client;
import org.praktika.api.entity.Employee;
import org.praktika.api.entity.OrderCity;
import org.praktika.api.entity.TurOrder;
import org.praktika.api.repository.CityRepository;
import org.praktika.api.repository.ClientRepository;
import org.praktika.api.repository.EmployeeRepository;
import org.praktika.api.repository.OrderCityRepository;
import org.praktika.api.repository.PostRepository;
import org.praktika.api.repository.TurOrderRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Random;


@Service
@RequiredArgsConstructor
public class GenerationDataService {
    private final PostRepository postRepository;
    private final EmployeeRepository employeeRepository;
    private final ClientRepository clientRepository;
    private final TurOrderRepository turOrderRepository;
    private final CityRepository cityRepository;
    private final OrderCityRepository orderCityRepository;


    @Transactional
    public void createDemoData(GenerationDataDto dto) {
        var generatorEmployee = new ExampleDataGenerator<>(Employee.class, LocalDateTime.now());
        generatorEmployee.setData(Employee::setFullName, DataType.FULL_NAME);
        generatorEmployee.setData(Employee::setLogin, DataType.EMAIL);
        var employees = generatorEmployee.create(dto.getGenerationNumber(), new Random().nextInt());
        var posts = postRepository.findAll();
        for (var employee : employees) {
            employee.setPost(posts.get(new Random().nextInt(posts.size())));
        }
        employeeRepository.saveAll(employees);

        var generatorClient = new ExampleDataGenerator<>(Client.class, LocalDateTime.now());
        generatorClient.setData(Client::setFullName, DataType.FULL_NAME);
        generatorClient.setData(Client::setDob, DataType.DATE_OF_BIRTH);
        generatorClient.setData(Client::setPhone, DataType.PHONE_NUMBER);
        clientRepository.saveAll(generatorClient.create(dto.getGenerationNumber(), new Random().nextInt()));

        var generatorTurOrder  = new ExampleDataGenerator<>(TurOrder.class, LocalDateTime.now());
        generatorTurOrder.setData(TurOrder::setDate, DataType.DATETIME_LAST_10_YEARS);
        generatorTurOrder.setData(TurOrder::setPrice, DataType.PRICE);
        var turOrders = generatorTurOrder.create(dto.getGenerationNumber(), new Random().nextInt());
        var ListEmployees  = employeeRepository.findAll();
        var ListClients = clientRepository.findAll();
        for (var turOrder : turOrders) {
            turOrder.setEmployee(ListEmployees.get(new Random().nextInt(ListEmployees.size())));
            turOrder.setClient(ListClients.get(new Random().nextInt(ListClients.size())));
        }
        turOrderRepository.saveAll(turOrders);

        var generatorCity = new ExampleDataGenerator<>(City.class, LocalDateTime.now());
        generatorCity.setData(City::setName, DataType.CITY);
        cityRepository.saveAll(generatorCity.create(dto.getGenerationNumber(), new Random().nextInt()));


        var generatorOrderCity = new ExampleDataGenerator<>(OrderCity.class, LocalDateTime.now());
        generatorOrderCity.setData(OrderCity::setDateStart, DataType.DATE_LAST_30_DAYS);
        generatorOrderCity.setData(OrderCity::setDateExpiration, DataType.DATE_NEXT_30_DAYS);
        var orderCities  = generatorOrderCity.create(dto.getGenerationNumber(), new Random().nextInt());
        var ListTurOrders = turOrderRepository.findAll();
        var ListCities  = cityRepository.findAll();
        for (var orderCitie : orderCities) {
            orderCitie.setTurOrder(ListTurOrders.get(new Random().nextInt(ListTurOrders.size())));
            orderCitie.setCity(ListCities.get(new Random().nextInt(ListCities.size())));
        }
        orderCityRepository.saveAll(orderCities);
    }
}
